package com.animeJavaExample;

import com.animeJava.core.AnimeJava;
import com.animeJava.dao.animeEvent.AnimeRender;
import com.animeJava.mode.PositionVarModel;
import com.animeJava.mode.animeModel.AnimeHoverProperty;
import com.animeJava.mode.animeModel.AnimeProperty;
import com.animeJava.util.AnimeException;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 作者: 韩万路
 * 创建时间: 2022年08月20日 下午 12:17
 * 类名: Anime06AutoExit
 * 公司: 华领科技
 * 地点: 苏州
 * 描述: 自动销毁
 */
public class Anime06AutoExit  extends Application {
    Button btn = null;
    Button btn1 = null;
    Button btn2 = null;
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setWidth(500);
        primaryStage.setHeight(500);

        AnchorPane root = new AnchorPane();
        primaryStage.setScene(new Scene(root));

        Button box = new Button("");
        root.getChildren().add(box);
        box.setPrefWidth(80);
        box.setPrefHeight(80);
        box.setLayoutX(40);
        box.setLayoutY(40);



        box.setOnAction(event -> {

            if(btn == null){
                new Thread(()->{
                    Button button = createButton();
                    Button button1 = createButton();
                    Button button2 = createButton();
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Platform.runLater(()->{
                        root.getChildren().add(button);
                        btn = button;
                        root.getChildren().add(button1);
                        btn1 = button1;
                        root.getChildren().add(button2);
                        btn2 = button2;
                    });
                }).start();
            }else{
                root.getChildren().remove(btn);
                btn = null;
                root.getChildren().remove(btn1);
                btn1 = null;
                root.getChildren().remove(btn2);
                btn2 = null;
            }



        });

        AnimeHoverProperty hoverProperty = new AnimeHoverProperty();
        hoverProperty.setDuration(300);
        hoverProperty.inPosition(1D);
        AnimeJava<AnimeHoverProperty> hover = new AnimeJava<>(hoverProperty);
        hover.update((timeTo, animeInfo, data) -> {
            if(data.get(0) != null){
                Platform.runLater(()->{
                    box.setScaleY(data.get(0));
                    box.setScaleX(data.get(0));
                });
            }
            return box.getScene() != null;
        });
        box.addEventHandler(MouseEvent.ANY, event -> {
            double scale = 1;
            if(event.getEventType() == MouseEvent.MOUSE_PRESSED){
                scale = 0.8;
                hover.playTo(scale);
            }
            if(event.getEventType() == MouseEvent.MOUSE_RELEASED){
                scale = 1;
                hover.playTo(scale);
            }


        });


        primaryStage.show();
    }


    private Button createButton(){
        Button button = new Button();

        button.setTranslateX(0);

        AnimeProperty animeProperty = new AnimeProperty();
        animeProperty.addKeyFrames(0D,button.getTranslateX());
        animeProperty.addKeyFrames(1D,30D);
        animeProperty.setAutoplay(true);
        animeProperty.setLoop(true);
        AnimeJava<AnimeProperty> animePropertyAnimeJava = new AnimeJava(animeProperty);
        final boolean[]               animeFlag = {false};
        animePropertyAnimeJava.update(new AnimeRender() {
            @Override
            public boolean run(Double timeTo, Map<String, PositionVarModel> animeInfo, List<Double> data)  {
                if(data.get(0) != null){
                    Platform.runLater(()->{
                        button.setTranslateX(data.get(0));
                    });
                }
                System.out.println("当前的button"+button.getScene() );
                System.out.println("当前data大小"+data.size()+"   当前节点的父节点:"+button.getScene());
                if(button.getScene() != null){
                    animeFlag[0] = true;
                }
                if(animeFlag[0]){
                    return button.getScene() != null;
                }
                return true;
            }
        });

        return button;
    }
}
